#define  _CRT_SECURE_NO_WARNINGS 1

#include"Stack.h"

void STInit(ST* stack)
{
	assert(stack);
	stack->a = (STDataType*)malloc(sizeof(STDataType)*INICAPA);
	if (stack->a == NULL) {
		perror("malloc fail");
		exit(-1);
	}
	stack->capacity = INICAPA;
	stack->top = 0;
}

void STDestroy(ST* stack)
{
	assert(stack);
	free(stack->a);
	stack->a = NULL;
	stack->top = 0;
	stack->capacity = 0;
}

void STPush(ST* stack, STDataType x)
{
	assert(stack);
	if (stack->top == stack->capacity) {
		STDataType* tmp = (STDataType*)realloc(stack->a, sizeof(STDataType) * stack->capacity * 2);
		if (tmp == NULL) {
			perror("realloc fail");
			exit(-1);
		}
		stack->a = tmp;
		stack->capacity *= 2;
	}
	stack->a[stack->top] = x;
	stack->top++;
}

void STPop(ST* stack)
{
	assert(stack);
	assert(!STEmpty(stack));

	stack->top--;
}

bool STEmpty(ST* stack)
{
	assert(stack);
	return stack->top == 0;
}
int STSize(ST* stack)
{
	assert(stack);
	return stack->top;
}

STDataType STTop(ST* stack)
{
	assert(stack);
	assert(!STEmpty(stack));
	return stack->a[stack->top - 1];
}